// pages/menu/region/editRegion/editRegion.js
var verify = require('../../../../utils/verify-token.js')
var $allcitys = require('../../../../utils/diqu.js');
var $citys = $allcitys.data;
var cityData = [];
for(var key in $citys){
	var obj = {};
	obj.name = key;
	obj.sub = [];	
	for(var kk in $citys[key]){	
		var tmobj = {};
		tmobj['name'] = kk;
		tmobj['sub'] = [];		
		for(var mm of $citys[key][kk]){
			var tnobj = {};
			tnobj.name = mm;
			tmobj['sub'].push(tnobj);
		}
		obj.sub.push(tmobj);
	}
	cityData.push(obj);
}

var provinces = [];
var citys = [];
var countys = [];
var address = '',provinceName = '', cityName='', countyName='';
//城市
for (let i = 0; i < cityData.length; i++) {
    provinces.push(cityData[i].name);
}
if(cityData[0].sub){
	for (let i = 0; i < cityData[0].sub.length; i++) {
	    citys.push(cityData[0].sub[i].name)
	}
	if(cityData[0].sub[0].sub){
		for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
		    countys.push(cityData[0].sub[0].sub[i].name)
		}
	}
}
provinceName = provinces[0] ;
cityName = citys[0] ;
countyName =  countys[0];
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList:[["a","b"],["c","d"]],
    index:0,   
    success:false,
    detailaddr: "",
    locationname: "",
		provinces: provinces,
		citys: citys,
		countys: countys,
		cityValue: [0,0,0],
		cityText:'请选择地址',
		isCity: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log("editRegion = ",options)    
		var cityValue = options.careless=="null"?[0,0,0]:options.careless.split(',');  
		citys = [];
		countys = [];		
		if(cityData[cityValue[0]].sub){			
			for (let i = 0; i < cityData[cityValue[0]].sub.length; i++) {
			  citys.push(cityData[cityValue[0]].sub[i].name)
			}			
			if(cityData[cityValue[0]].sub.length>0){				
				for (let i = 0; i < cityData[cityValue[0]].sub[cityValue[1]].sub.length; i++) {
				  countys.push(cityData[cityValue[0]].sub[cityValue[1]].sub[i].name)
				}
			}
		}		
		provinceName = options.province;
		cityName = options.city;
		countyName = options.area;
    var public1
    if(options.public=="true"){
      public1 = true
    }else{
      public1 = false
    }
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      companyname: options.companyname,
      cityValue: cityValue,
      cityText:provinceName+' '+cityName+' '+countyName,
      locationname: options.locationname,
      detailaddr: options.detailaddr,
      public1: public1, 
      locationid: options.locationid,
      regionid: options.regionid,
			citys: citys,
			countys: countys,
    })
  },
	
		risePicker: function(e){	
	    var that = this;
	    var $mold = e.currentTarget.dataset.mold;
	    if($mold == 'city'){
	        that.setData({
	            isCity: false
	        })
	    }
	    
	},
	 //城市选择器
	cityChange: function (e) {
		console.log(e)
	   var val = e.detail.value
	   var t = this.data.cityValue;     
	   if (val[0] != t[0]) {
	       citys = [];
	       countys = [];
				 if(cityData[val[0]].sub){
					for (let i = 0; i < cityData[val[0]].sub.length; i++) {
					  citys.push(cityData[val[0]].sub[i].name)
					}
					//console.log(cityData[val[0]].sub)
					if(cityData[val[0]].sub.length>0){
						for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
						  countys.push(cityData[val[0]].sub[0].sub[i].name)
						} 
					}
				 }         
	       
	       this.setData({
	           citys: citys,
	           countys: countys,
	           cityValue: [val[0], 0, 0]
	       })
	       provinceName = cityData[val[0]].name;
	       cityName = cityData[val[0]].sub[0]?cityData[val[0]].sub[0].name:'';
	       countyName = cityData[val[0]].sub[0]?cityData[val[0]].sub[0].sub[0]?cityData[val[0]].sub[0].sub[0].name:'':'';
	       address += provinceName + " " + cityName + " " + countyName; 
	       return;
	   }
	   if (val[1] != t[1]) {
	       countys = [];
				 if(cityData[val[0]].sub[val[1]].sub){
					for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
					    countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
					} 
				 }
	       
	       this.setData({
	           countys: countys,
	           cityValue: [val[0], val[1], 0]
	       })
	       cityName = cityData[val[0]].sub[val[1]].name;
	       countyName = cityData[val[0]].sub[val[1]].sub[0].name;
	       address += cityData[val[0]].name + " " + cityData[val[0]].sub[val[1]].name + " " + cityData[val[0]].sub[val[1]].sub[0].name; 
	       return;
	   }
	   if (val[2] != t[2]) {
	       this.setData({
	           county: this.data.countys[val[2]],
	           cityValue: val
	       })
	       countyName = cityData[val[0]].sub[val[1]].sub[val[2]].name;
	       address += cityData[val[0]].name + " " + cityData[val[0]].sub[val[1]].name + " " + cityData[val[0]].sub[val[1]].sub[val[2]].name;
	       return;
	   }
	   
	 
	},
	 //确定选择
	 ideChoice: function(e){
			 var that = this;
			 var $act = e.currentTarget.dataset.act;
			 var $mold = e.currentTarget.dataset.mold;	
			 //城市
			 if ($act == 'confirm' && $mold == 'city') {		 
					 that.setData({
							cityText: provinceName + ' ' + cityName + ' ' + countyName,
					 })
			 }
			 that.setData({
					 isCity:true        
			 })
	 },

  // 修改点位名称
editName:function(res){
  console.log("editName", res.detail.value)
  this.setData({
    locationname: res.detail.value
  })
},

// 修改点位地址
  selectLocation:function(res){
    console.log("selectLocation", res)
    var province = res.detail.value[0]
    var city = res.detail.value[1]
    var area = res.detail.value[2]
    this.setData({
      province: province,
      city: city,
      area: area,
    })
  },

// 修改点位具体地址
  editDetailaddr:function(res){
    console.log("editDetailaddr",res.detail.value)
    this.setData({
      detailaddr: res.detail.value
    })
  },

// 是否公开
  isPublic: function (res) {
    console.log("isPublic", res.detail.value)
    var public1 = true
    if (res.detail.value == "false") {
      public1 = false
    }
    this.setData({
      public1: public1
    })
  },

// 点击保存
  editLocation:function(){    
    var detailaddr = this.data.detailaddr
    var locationname = this.data.locationname
    var pictures = []    
    var public1 = this.data.public1
    var regionid = this.data.regionid
    var locationid = this.data.locationid
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this

    if (locationname == "") {
      wx.showModal({
        title: '点位名称为空！',
        content: '请输入点位名称！！',
      })
    } else if (this.data.cityText=="请选择地址") {
      wx.showModal({
        title: '错误的点位地址！',
        content: '请选择正确的点位地址！！',
      })
    }else if (detailaddr == "") {
      wx.showModal({
        title: '点位详细地址为空！',
        content: '请输入街道门牌号！！',
      })
    }else {
      wx.request({
        url: baseUrl+'location/'+locationid,
        method:"put",
        header: {
          'AUTHORIZATION': token
        },
        data: {
          "address": this.data.cityText.replace(/\s+/g,"")+detailaddr,
          "area": countyName,
          "city": cityName,
          "detailaddr": detailaddr,
          "locationName": locationname,
          "pictures": pictures,
          "province": provinceName,
          "public": public1,
          "regionId": regionid,
					"careless":JSON.stringify(this.data.cityValue)
        },
        success:function(res){
         // console.log("success",res)
          if (res.data.code == 60006) {
            verify.clearAll()
          } else if(res.data.code==200){
            wx.showToast({
              title: '修改成功!',
              duration: 3500
            })
						
            that.setData({
              success:true,
							cityText:res.data.data.province+' '+res.data.data.city+' '+res.data.data.area,
							detailaddr:res.data.data.detailaddr							
            })
          }else{
            wx.showModal({
              title: '保存失败',
              content: res.data.message,
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})